package com.jprocms.module.cms.controller.directive;

import com.jpro.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jprocms.module.cms.controller.admin.investigate.vo.InvestigateListReqVO;
import com.jprocms.module.cms.dal.dataobject.investigate.InvestigateDO;
import com.jprocms.module.cms.service.investigate.InvestigateService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.*;

import static com.jprocms.module.cms.web.Directives.*;

/**
 * 调查问卷列表 标签
 *
 * @author jprocms
 */
public class InvestigateListDirective implements TemplateDirectiveModel {
    /**
     * 是否在投票期限内。布尔型(Boolean)。默认：全部。
     */
    public static final String IS_ACTIVE = "isActive";
    /**
     * 站点ID。整型(Integer)。
     */
    public static final String SITE_ID = "siteId";
    /**
     * 是否获取所有站点的数据。布尔型(Boolean)。默认：false。
     */
    public static final String ALL_SITE = "allSite";

    public static void assemble(InvestigateListReqVO args, Map<String, ?> params, Long defaultSiteId) {
        Long siteId = getLong(params, SITE_ID);
        // 不获取所有站点，则给默认站点ID
        if (siteId == null && !getBoolean(params, ALL_SITE, false)) {
            siteId = defaultSiteId;
        }
        args.setSiteId(siteId);
        Optional.ofNullable(getBoolean(params, IS_ACTIVE)).ifPresent(args::setIsActive);
        args.setEnabled(true);
        Collection<OrderItem> orderItems = getOrderItems(Arrays.asList("sort_num_asc", "id_asc"));
        args.setOrderItems(orderItems);
    }

    protected void doExecute(Environment env, Map<String, TemplateModel> params, TemplateModel[] loopVars,
                             TemplateDirectiveBody body, boolean isPage) throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);
        InvestigateListReqVO args = new InvestigateListReqVO();
        assemble(args, params, FrontContextUtil.getSiteId(env));
        if (isPage) {
            int page = Directives.getPage(params, env);
            int pageSize = Directives.getPageSize(params, env);
            args.setPageSize(pageSize);
            args.setPageNo(page);
            PageResult<InvestigateDO> votePage = service.getInvestigatePage(args);
            Directives.setTotalPages(votePage.getTotalPages());
            loopVars[0] = env.getObjectWrapper().wrap(votePage);
        } else {
            int offset = Directives.getOffset(params);
            int limit = Directives.getCount(params);
            args.setCount(limit);
            args.setOffset(offset);
            List<InvestigateDO> list = service.getInvestigateList(args);
            loopVars[0] = env.getObjectWrapper().wrap(list);
        }
        body.render(env.getOut());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        doExecute(env, params, loopVars, body, false);
    }

    private final InvestigateService service;

    public InvestigateListDirective(InvestigateService service) {
        this.service = service;
    }
}
